package com.lovo.roomProject.classz.dao.impl;

import com.lovo.roomProject.classz.dao.ClasszDAO;
import com.lovo.roomProject.dao.BaseDAO;
import com.lovo.roomProject.pojo.ClasszDO;
import com.lovo.roomProject.pojo.CutPageBean;
import com.lovo.roomProject.pojo.RoomDO;
import com.lovo.roomProject.pojo.StudentDO;

import java.sql.SQLException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

public class ClasszDAOImpl extends BaseDAO implements ClasszDAO {
    @Override
    public List listClassz() {
        this.getConnection();
        String sql="select * from t_class";
        List list =new ArrayList();
        try {
            statement=con.prepareStatement(sql);
            rs=statement.executeQuery();
            while (rs.next()){
                ClasszDO classzDO=this.fillClassz();
                list.add(classzDO);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            close(rs,statement,con);
        }
        return list;
    }

    @Override
    public void insert(ClasszDO classzDO) {
        this.getConnection();
        String sql="INSERT INTO t_class (c_name,c_teacher,c_create_date) VALUES (?,?,?)";
        try {
            statement=con.prepareStatement(sql);
            statement.setObject(1,classzDO.getName());
            statement.setObject(2,classzDO.getTeacher());
            statement.setObject(3,classzDO.getCreateDate());
            statement.executeUpdate();
        }catch (SQLException e){
            e.printStackTrace();
        }finally{
            close();
        }
    }

    @Override
    public void delete(int id) {
        this.getConnection();
        String sql="delete from t_class where c_id=?";
        try {
            statement=con.prepareStatement(sql);
            statement.setObject(1,id);
            statement.executeUpdate();
        }catch (SQLException e){
            e.printStackTrace();
        }finally{
            close();
        }
    }

    @Override
    public CutPageBean listClasszPage(int current) {
        this.getConnection();
        String selectSql="select * ";
        String countSql="select count(*) cnt ";
        String fromSql="from t_class ";
        String limitSql="limit ?,? ";
        CutPageBean cutPageBean=new CutPageBean<>();
        List list=new ArrayList();
        try {
            statement=con.prepareStatement(selectSql+fromSql+limitSql);
            statement.setObject(1,(current-1)*CutPageBean.PAGESIZE);
            statement.setObject(2,CutPageBean.PAGESIZE);
            rs=statement.executeQuery();
            while (rs.next()){
                ClasszDO classzDO=fillClassz();
                list.add(classzDO);
            }
            cutPageBean.setList(list);
            // 计算总数的SQL
            statement=con.prepareStatement(countSql+fromSql);
            rs=statement.executeQuery();
            rs.next();
            cutPageBean.setCount(rs.getInt("cnt"));
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            close(rs,statement,con);
        }
        return cutPageBean;
    }

    @Override
    public ClasszDO fillClassz() throws SQLException {
        ClasszDO classzDO=new ClasszDO();
        classzDO.setId(rs.getInt("c_id"));
        classzDO.setName(rs.getString("c_name"));
        classzDO.setTeacher(rs.getString("c_teacher"));
        classzDO.setCreateDate(LocalDate.parse(rs.getString("c_create_date")));
        return classzDO;
    }
}
